<?php
class AdministratorAction extends CommonAction{
      /*+-------------------------------
        |    添加管理员
        +-------------------------------
       */
      public function addAdmin(){
             if($_POST['create']){
                   $data['sysuser_name']     = $_POST['sysuser_name'];
                   $data['sysuser_password'] = md5($_POST['sysuser_name']);
                   $data['sysuser_email']    = $_POST['sysuser_email'];
                   $data['sysuser_describe'] = $_POST['sysuser_describe'];
                   $data['create_user']      = $_SESSION[C("USER_AUTH_KEY")];
                   $data['create_time']      = toDate(time());
                   $data['user_type']        = "1";
                   //echo "<pre>";
                   //print_r($data);
                   $admin = D("sysuser");
                   $add   = $admin -> add($data);
                   if($add){
                          $this->success("添加管理员成功！");
                   }else{
                          $this->error("添加管理员失败！");
                   }
             }        
             $this->display();
      }

      /*+-------------------------------
        |    查看管理员
        +-------------------------------
       */
      public function showAdmin(){
          $order = "create_time desc";
          $limit = $this->pageOperate("sysuser","15");
          $this->getGroupInfo($this->query($order,$limit['firstRow'].",".$limit['listRows']));
          $this->display();
      }

      /*+-------------------------------
        |    修改管理员
        +-------------------------------
       */

      public function editAdmin(){
             $order = "create_time desc";
             $limit = $this->pageOperate("sysuser","15");
             $this->getGroupInfo($this->query($order,$limit['firstRow'].",".$limit['listRows']));
             $this->display();
      }

      /*+-------------------------------
        |    执行编辑管理员
        +-------------------------------
       */
      public function executeEdit(){
             //$data  = $_POST;
             $admin = D("sysuser");
             if($_GET['Gid']){
                 $where = "id = {$_GET['Gid']}";
             }
             if($_POST['show']){
                 foreach($_POST['id'] as $key => $val){
                       $in .=$val.",";       
                 }
                 $in    = rtrim($in,",");
                 $where = "id in ({$in})";
             }
             $where .= " and user_type = '1' and status = '1'";
             $adminArray = $admin ->where($where) ->select();
             $this->assign("adminArray",$adminArray);
             if($_POST['edit']){
                
                 foreach($_POST as $key => $val){
                         if(is_array($val)){
                             foreach($val as $k => $v){
                                      if($key == "sysuser_password"){
                                          if($v != ""){
                                             $data[$k][$key] = md5($v);
                                          }
                                      }else{         
                                             $data[$k][$key] = $v;
                                      }
                             }
                         }
                 }
                 //编辑
                 foreach($data as $up){
                         $update +=$admin ->save($up);
                 }
                 if($update){
                         $this->success("成功的修改了，{$update} 个管理员信息!");
                 }else{
                         $this->error("修改管理员信息失败！");
                 }
             }
             //$edit  = $admin ->save();
             //if($edit){
            //        $this -> success("管理员信息编辑成功！");
             //}else{
             //       $this -> error("管理员信息编辑失败！");
             //}
             $this->display();
      }

      /*+-------------------------------
        |    删除管理员
        +-------------------------------
       */
      public function delAdmin(){
             $order = "create_time desc";
             $limit = $this->pageOperate("sysuser","50");
             $this->getGroupInfo($this->query($order,$limit['firstRow'].",".$limit['listRows']));
             $this->display();
      }

       //多表查询语句、
    public function query($order,$limit){
        $query = "select a.id,a.sysuser_name,a.sysuser_describe,a.sysuser_nikename,a.sysuser_email,b.sysuser_nikename create_user,a.create_time from douhao_sysuser a,douhao_sysuser b where a.create_user = b.id and a.status = '1' and a.user_type = '1'";
        if($order){
             $query .= " order by a.".$order;

        }
        if($limit){
            $query .= " limit ".$limit;
        }
        return $query;

    }

    /*+-----------------------------------
      |      执行删除管理员
      +-----------------------------------
     */
    public function executeDel(){
        	 $admin = D("sysuser");
             if($_GET['Gid']){
                 $where = "id = {$_GET['Gid']}";
             }
             if($_POST['show']){
                 foreach($_POST['id'] as $key => $val){
                       $in .=$val.",";       
                 }
                 $in    = rtrim($in,",");
                 $where = "id in ({$in})";
             }
			 
			 $del = $admin ->where($where)->delete();
			 if($del){
				      $this->success("成功的删除了 {$del} 个管理员!");
			 }else{
					  $this->error("删除管理员失败！");
			 }
			 
    }


}
?>
